/***************************************************************************
 *  Filename: main.cpp
 *  Class:
 *
 *  Copyright 2012  Jacco van der Spek
 *  <jaccovdspek@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of FireFleye Main Controller.
 *
 *    FireFleye is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    FireFleye is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with FireFleye.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtCore/QCoreApplication>

#include <QDebug>
#include <QStringList>
#include <QThread>
#include <QDateTime>
#include <QFile>
#include <QTextStream>

#include <gpshandler.h>
#include <barohandler.h>
#include <parrothandler.h>
#include <videoprocessing.h>
#include <groundscommunicator.h>

#include "mainloopfunctions.h"
#include "definitions.h"

#define LOGLEVEL QtWarningMsg

void myMessageHandler(QtMsgType type, const char *msg)
{
    QString txt;

    switch (type) {
    case QtDebugMsg:
        txt = QString("Debug: %1 %2").arg(QDateTime::currentDateTime().toString(), msg);
        break;
    case QtWarningMsg:
        txt = QString("Warning: %1 %2").arg(QDateTime::currentDateTime().toString(), msg);
        break;
    case QtCriticalMsg:
        txt = QString("Critical: %1").arg(msg);
        break;
    case QtFatalMsg:
        txt = QString("Fatal: %1").arg(msg);
        abort();
    }

    if(type >= LOGLEVEL){
        QFile outFile("log");
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream ts(&outFile);
        ts << txt << endl;
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qInstallMsgHandler(myMessageHandler);

    BaroHandler baroTest;
    GPSHandler gpsTest(USB_GPS);
    ParrotHandler parrot(USB_PARROT);
    GroundSCommunicator xbee(USB_XBEE);

    VideoProcessing video;
    QThread *videoThread = new QThread(&a);
    video.moveToThread(videoThread);
    video.init();
    QObject::connect(videoThread, SIGNAL(started()), &video, SLOT(run()));
    videoThread->start();

    QThread *gpsThread = new QThread(&a);
    gpsTest.moveToThread(gpsThread);
    gpsThread->start();

    QThread *baroThread = new QThread(&a);
    baroThread->start();
    baroTest.moveToThread(baroThread);
    baroTest.setReference();
    baroTest.runHandler();

    MainLoopFunctions func(&baroTest, &gpsTest, &parrot, &xbee, &video);
    func.startPictureTimer(15);
    func.startSenderTimer(2000);
    func.startMainLoopTimer(50);
    return a.exec();
}
